/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import uber.netbsd.community.dao.Person;
import uber.netbsd.community.dao.PersonManager;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.action.UrlBinding;
import java.util.List;
import net.sourceforge.stripes.action.DontValidate;

/**
 * This code was taken from stripes samples
 * Manages the administration of People
 *
 * @author Tim Fennell
 * @author alex
 */
@UrlBinding("/action/AdministerPeople")
public class AdministerPeopleActionBean extends BaseActionBean {

    private int[] deleteIds;
    @ValidateNestedProperties({
        @Validate(field = "username", required = true, minlength = 3, maxlength = 15),
        @Validate(field = "password", minlength = 6, maxlength = 20),
        @Validate(field = "firstName", required = true, maxlength = 25),
        @Validate(field = "lastName", required = true, maxlength = 25),
        @Validate(field = "email", converter = EmailTypeConverter.class)
    })
    private List<Person> people;

    public int[] getDeleteIds() {
        return deleteIds;
    }

    public void setDeleteIds(int[] deleteIds) {
        this.deleteIds = deleteIds;
    }

    public List<Person> getPeople() {
        return people;
    }

    public void setPeople(List<Person> people) {
        this.people = people;
    }

    @HandlesEvent("New")
    @DontValidate
    public Resolution createNew() {

        return new RedirectResolution("/action/Profile").addParameter("preEdit", "");
    }

    @HandlesEvent("Save")
    @DefaultHandler
    public Resolution saveChanges() {
        PersonManager pm = PersonManager.getInstance();

        // Apply any changes to existing people (and create new ones)
        for (Person person : people) {
            Person realPerson;
            if (person.getId() == null) {
                realPerson = new Person();
            } else {
                realPerson = pm.getPerson(person.getId());
            }

            realPerson.setEmail(person.getEmail());
            realPerson.setFirstName(person.getFirstName());
            realPerson.setLastName(person.getLastName());
            realPerson.setUsername(person.getUsername());

            if (person.getPassword() != null) {
                realPerson.setPassword(person.getPassword());
            }

            pm.saveOrUpdate(realPerson);
        }

        // Then, if the user checked anyone off to be deleted, delete them
        if (deleteIds != null) {
            for (int id : deleteIds) {
                pm.deletePerson(id);
            }
        }

        return new RedirectResolution("/admin/admin.jsp");
    }
}
